package com.huishu.user.utils;


import com.huishu.user.pojo.vo.MenuVO;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 递归工具类
 *
 * @param
 */
public class RecursiveUtils {

    /**
     * 遍历权限菜单
     *
     * @param list
     * @return
     */
    public static List<MenuVO> getMenu(List<MenuVO> list) {
        //用递归找子。
        List<MenuVO> treeList = new ArrayList<>();
        for (MenuVO tree : list) {
            if (tree.getMenuPid() == null) {
                treeList.add(findChildren(tree, list));
            }
        }
        return treeList;
    }

    private static MenuVO findChildren(MenuVO tree, List<MenuVO> list) {
        for (MenuVO node : list) {
            if (tree.getId()== node.getMenuPid()) {
                if (tree.getChildren() == null) {
                    tree.setChildren(new ArrayList<>());
                }
                tree.getChildren().add(findChildren(node, list));
            }
        }
        return tree;
    }

}
